﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
//using System.Threading;


namespace AutoMouseClick
{
    public partial class Form1 : Form
    {
        private DataWareHouse dataWH = new DataWareHouse();
        private string fromLst = "";
        private const string DANGNHAP = "Đăng nhập";
        private Thread runThread;
        private RunClick rClk = new RunClick();
        private bool isProcessing = false;
        private string copyFrom = "";

        public Form1()
        {
            InitializeComponent();
            System.IO.Directory.SetCurrentDirectory(Application.StartupPath);
            //Load database
            dataWH.LoadData();
            dataWH.LoadBlackList();
            dataWH.LoadWhiteList();

            MyInit();

            lstV_Pos.Items.Clear();
            foreach (DataRow dr in dataWH.DT.Rows)
            {
                lstV_Pos.Items.Add(CreateLI((string)dr[0], (string)dr[1]));
            }

            lstV_TaskLevel4.Items.Clear();
            for (int i = 0; i < dataWH.LST_TSKLVL4.Count; i++)
            {
                if (i == dataWH.TASKLOGININDEX)
                    lstV_TaskLevel4.Items.Add((CreateLI(DANGNHAP, "1")));
                else
                {
                    TaskLevel4 tskLvl4 = (TaskLevel4)dataWH.LST_TSKLVL4[i];
                    lstV_TaskLevel4.Items.Add(CreateLI(tskLvl4.name, tskLvl4.arrTskLvl3_Count));
                }
            }
        }

        private void MyInit()
        {
            txtB_WaitPos.Text = "2000";
            txtB_ClickInterval.Text = txtB_ClickIntervalDefault.Text;
            txtB_ClickNumber.Text = "1";
            txtB_ClickOffset.Text = "0";
            txtB_ClickOffsetNumber.Text = "0";
            txtB_Task1Number.Text = "1";
            txtB_Task2Number.Text = "1";
            txtB_Task3Number.Text = "1";
            txtB_Task4Number.Text = dataWH.RUNNUMBER;

            txtB_ClickIntervalDefault.Text = dataWH.PROGSETS.clickIntervalDefault;
            txtB_TaskInterval.Text = dataWH.PROGSETS.taskInterval;

            //databinding
            cB_PosUsername.BindingContext = new BindingContext();
            cB_PosUsername.DataSource = dataWH.DT;
            cB_PosUsername.DisplayMember = "Tên Tọa Độ";

            cB_PostLogin.BindingContext = new BindingContext();
            cB_PostLogin.DataSource = dataWH.DT;
            cB_PostLogin.DisplayMember = "Tên Tọa Độ";

            cB_PosLogOut.BindingContext = new BindingContext();
            cB_PosLogOut.DataSource = dataWH.DT;
            cB_PosLogOut.DisplayMember = "Tên Tọa Độ";

            cB_PosClick.BindingContext = new BindingContext();
            cB_PosClick.DataSource = dataWH.DT;
            cB_PosClick.DisplayMember = "Tên Tọa Độ";

        }

        #region Notify Icon
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        private void menu_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void btnGetPosition_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(int.Parse(txtB_WaitPos.Text));
            Point pos = System.Windows.Forms.Cursor.Position;
            txtB_Pos.Text = pos.X + "," + pos.Y;
            this.WindowState = FormWindowState.Normal;
        }

        private void btnInsertPosition_Click(object sender, EventArgs e)
        {
            if (txtB_PosName.Text == "" || txtB_Pos.Text == "") return;

            if (!lstV_Pos.Items.ContainsKey(txtB_PosName.Text))
            {
                dataWH.Add(txtB_PosName.Text, txtB_Pos.Text);

                lstV_Pos.Items.Add(CreateLI(txtB_PosName.Text, txtB_Pos.Text));
                lstV_Pos.EnsureVisible(lstV_Pos.Items.Count - 1);
            }
            else
            {
                int idx = lstV_Pos.Items[txtB_PosName.Text].Index;
                dataWH.Delete(idx);
                dataWH.Insert(idx, txtB_PosName.Text, txtB_Pos.Text);

                lstV_Pos.Items.RemoveByKey(txtB_PosName.Text);
                lstV_Pos.Items.Insert(idx,CreateLI(txtB_PosName.Text, txtB_Pos.Text));
                lstV_Pos.EnsureVisible(idx);
            }
         
            txtB_PosName.Text = "";
            txtB_Pos.Text = "";
        }

        private void lstV_Pos_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isProcessing) return;

            ListView lv = (ListView)sender;
            if (lv.SelectedItems.Count != 1) return;

            txtB_PosName.Text = lv.SelectedItems[0].Text;
            txtB_Pos.Text = lv.SelectedItems[0].SubItems[1].Text;
        }

        private void lstV_Pos_KeyDown(object sender, KeyEventArgs e)
        {
            ListView lv = (ListView)sender;
            isProcessing = true;
            switch (e.KeyCode)
            {
                case Keys.Delete:
                    foreach (ListViewItem li in lv.SelectedItems)
                    {
                        dataWH.Delete(li.Index);
                        lv.Items.Remove(li);
                    }
                    break;
            }
            isProcessing = false;
        }

        private void lstV_Pos_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListView lv = (ListView)sender;
            lv.DoDragDrop(lv.SelectedItems, DragDropEffects.Move);
            fromLst = lv.Name;
        }

        private void lstV_Pos_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void lstV_Pos_DragDrop(object sender, DragEventArgs e)
        {
            ListView lv = (ListView)sender;
            if (lv.Name != fromLst)
                return;

            Point cp = lv.PointToClient(new Point(e.X, e.Y));
            ListViewItem dragToItem = lv.GetItemAt(cp.X, cp.Y);
            int dropIndex = -1;
            if (dragToItem != null)
                dropIndex = dragToItem.Index;
            else
                dropIndex = (lv.Items.Count == 0) ? 0 : lv.Items.Count - 1;

            int idx = dropIndex;

            foreach (ListViewItem li in (ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)))
            {
                dataWH.Delete(li.Index);
                dataWH.Insert(dropIndex, li.Text, li.SubItems[1].Text);

                if (li.Index > dropIndex)
                {
                    idx = dropIndex;
                    dropIndex++;
                }
                li.Remove();
                lv.Items.Insert(idx, li);
            }
        }

        private ListViewItem CreateLI(string col1, string col2)
        {
            ListViewItem li = new ListViewItem();
            li.Text = col1;
            li.Name = col1;

            ListViewItem.ListViewSubItem lsi = new ListViewItem.ListViewSubItem();
            lsi = new ListViewItem.ListViewSubItem();
            lsi.Text = col2;
            li.SubItems.Add(lsi);

            return li;
        }

        private void btn_ExportLogin_Click(object sender, EventArgs e)
        {
            if (txtB_WebAddress.Text == "" || cB_PosUsername.SelectedIndex < 0 || txtB_Username.Text == "" ||
                txtB_Password.Text == "" || cB_PostLogin.SelectedIndex < 0 || cB_PosLogOut.SelectedIndex < 0)
                return;
            string name = DANGNHAP;

            if (!lstV_TaskLevel4.Items.ContainsKey(name))
            {
                dataWH.ExportLoginInfo(name, txtB_WebAddress.Text, cB_PosUsername.Text, txtB_Username.Text, txtB_Password.Text, cB_PostLogin.Text, cB_PosLogOut.Text);
                lstV_TaskLevel4.Items.Add(CreateLI(name, "1"));
            }
            else
            {
                int idx = lstV_TaskLevel4.Items.IndexOfKey(DANGNHAP);
                dataWH.ModifyLoginInfo(idx,name, txtB_WebAddress.Text, cB_PosUsername.Text, txtB_Username.Text, txtB_Password.Text, cB_PostLogin.Text, cB_PosLogOut.Text);
            }
        }

        private void btn_ExportTask1_Click(object sender, EventArgs e)
        {
            if (txtB_TaskName.Text == "" || cB_PosClick.SelectedIndex < 0 || txtB_ClickNumber.Text == "" ||
                txtB_ClickInterval.Text == "" || txtB_ClickOffset.Text == "" || txtB_ClickOffsetNumber.Text == "")
                return;

            if (!lstV_TaskLevel1.Items.ContainsKey(txtB_TaskName.Text))
            {
                dataWH.ExportTaskLevel1(txtB_TaskName.Text, cB_PosClick.Text, txtB_ClickNumber.Text, txtB_ClickInterval.Text, txtB_ClickOffset.Text, txtB_ClickOffsetNumber.Text);
                
                lstV_TaskLevel1.Items.Add(CreateLI(txtB_TaskName.Text, cB_PosClick.Text));
                lstV_TaskLevel1.EnsureVisible(lstV_TaskLevel1.Items.Count - 1);
            }
            else
            {
                int idx = lstV_TaskLevel1.Items.IndexOfKey(txtB_TaskName.Text);
                dataWH.ModifyTaskLevel1(idx, txtB_TaskName.Text, cB_PosClick.Text, txtB_ClickNumber.Text, txtB_ClickInterval.Text, txtB_ClickOffset.Text, txtB_ClickOffsetNumber.Text);

                isProcessing = true;
                lstV_TaskLevel1.Items.RemoveAt(idx);
                lstV_TaskLevel1.Items.Insert(idx,CreateLI(txtB_TaskName.Text, cB_PosClick.Text));
                lstV_TaskLevel1.EnsureVisible(idx);
                isProcessing = false;
            }

            if (lstV_TaskLevel2.Items.ContainsKey(txtB_Task1Name.Text))
            {
                int idx2 = lstV_TaskLevel2.Items[txtB_Task1Name.Text].Index;
                dataWH.ModifyTaskLevel2(idx2, txtB_Task1Name.Text, txtB_Task1Number.Text);
                if (lstV_TaskLevel3.Items.ContainsKey(txtB_Task2Name.Text))
                {
                    int idx3 = lstV_TaskLevel3.Items[txtB_Task2Name.Text].Index;
                    dataWH.ModifyTaskLevel3(idx3, txtB_Task2Name.Text, txtB_Task2Number.Text);
                    if (lstV_TaskLevel4.Items.ContainsKey(txtB_Task3Name.Text))
                    {
                        int idx4 = lstV_TaskLevel4.Items[txtB_Task3Name.Text].Index;
                        dataWH.ModifyTaskLevel4(idx4, txtB_Task3Name.Text, txtB_Task3Number.Text);
                    }
                }
            }

            if (checkB_TaskReset.Checked == true)
            {
                txtB_TaskName.Text = "";
                cB_PosClick.SelectedIndex = -1;
                txtB_ClickNumber.Text = "1";
                txtB_ClickInterval.Text = txtB_ClickIntervalDefault.Text;
                txtB_ClickOffset.Text = "0";
                txtB_ClickOffsetNumber.Text = "0";
            }
        }

        private void btn_ExportTask2_Click(object sender, EventArgs e)
        {
            if (lstV_TaskLevel1.Items.Count == 0 || txtB_Task1Name.Text == "" || txtB_Task1Number.Text == "")
                return;

            if (!lstV_TaskLevel2.Items.ContainsKey(txtB_Task1Name.Text))
            {
                dataWH.ExportTaskLevel2(txtB_Task1Name.Text, txtB_Task1Number.Text);
                lstV_TaskLevel2.Items.Add(CreateLI(txtB_Task1Name.Text, txtB_Task1Number.Text));
                lstV_TaskLevel2.EnsureVisible(lstV_TaskLevel2.Items.Count - 1);
            }
            else
            {
                int idx = lstV_TaskLevel2.Items.IndexOfKey(txtB_Task1Name.Text);
                dataWH.ModifyTaskLevel2(idx,txtB_Task1Name.Text,txtB_Task1Number.Text);

                isProcessing = true;
                lstV_TaskLevel2.Items.RemoveAt(idx);
                lstV_TaskLevel2.Items.Insert(idx, CreateLI(txtB_Task1Name.Text, txtB_Task1Number.Text));
                lstV_TaskLevel2.EnsureVisible(idx);
                isProcessing = false;
            }

            if (lstV_TaskLevel3.Items.ContainsKey(txtB_Task2Name.Text))
            {
                int idx3 = lstV_TaskLevel3.Items[txtB_Task2Name.Text].Index;
                dataWH.ModifyTaskLevel3(idx3, txtB_Task2Name.Text, txtB_Task2Number.Text);
                if (lstV_TaskLevel4.Items.ContainsKey(txtB_Task3Name.Text))
                {
                    int idx4 = lstV_TaskLevel4.Items[txtB_Task3Name.Text].Index;
                    dataWH.ModifyTaskLevel4(idx4, txtB_Task3Name.Text, txtB_Task3Number.Text);
                }
            }

            dataWH.ClearTaskLevel1();
            lstV_TaskLevel1.Items.Clear();
            txtB_Task1Name.Text = "";
            txtB_Task1Number.Text = "1";
        }

        private void btn_ExportTask3_Click(object sender, EventArgs e)
        {
            if (lstV_TaskLevel2.Items.Count == 0 || txtB_Task2Name.Text == "" || txtB_Task2Number.Text == "")
                return;

            if (!lstV_TaskLevel3.Items.ContainsKey(txtB_Task2Name.Text))
            {
                dataWH.ExportTaskLevel3(txtB_Task2Name.Text, txtB_Task2Number.Text);
                lstV_TaskLevel3.Items.Add(CreateLI(txtB_Task2Name.Text, txtB_Task2Number.Text));
                lstV_TaskLevel3.EnsureVisible(lstV_TaskLevel3.Items.Count - 1);
            }
            else
            {
                int idx = lstV_TaskLevel3.Items.IndexOfKey(txtB_Task2Name.Text);
                dataWH.ModifyTaskLevel3(idx, txtB_Task2Name.Text, txtB_Task2Number.Text);

                isProcessing = true;
                lstV_TaskLevel3.Items.RemoveAt(idx);
                lstV_TaskLevel3.Items.Insert(idx, CreateLI(txtB_Task2Name.Text, txtB_Task2Number.Text));
                lstV_TaskLevel3.EnsureVisible(idx);
                isProcessing = false;
            }

            if (lstV_TaskLevel4.Items.ContainsKey(txtB_Task3Name.Text))
            {
                int idx4 = lstV_TaskLevel4.Items[txtB_Task3Name.Text].Index;
                dataWH.ModifyTaskLevel4(idx4, txtB_Task3Name.Text, txtB_Task3Number.Text);
            }

            dataWH.ClearTaskLevel1();
            lstV_TaskLevel1.Items.Clear();
            txtB_Task1Name.Text = "";
            txtB_Task1Number.Text = "1";

            dataWH.ClearTaskLevel2();
            lstV_TaskLevel2.Items.Clear();
            txtB_Task2Name.Text = "";
            txtB_Task2Number.Text = "1";
        }

        private void btn_ExportTask4_Click(object sender, EventArgs e)
        {
            if (lstV_TaskLevel3.Items.Count == 0 || txtB_Task3Name.Text == "" || txtB_Task3Number.Text == "")
                return;

            if (!lstV_TaskLevel4.Items.ContainsKey(txtB_Task3Name.Text))
            {
                dataWH.ExportTaskLevel4(txtB_Task3Name.Text, txtB_Task3Number.Text);
                lstV_TaskLevel4.Items.Add(CreateLI(txtB_Task3Name.Text, txtB_Task3Number.Text));
                //lstV_TaskLevel4.EnsureVisible(lstV_TaskLevel4.Items.Count - 1);
            }
            else
            {
                int idx = lstV_TaskLevel4.Items.IndexOfKey(txtB_Task3Name.Text);
                dataWH.ModifyTaskLevel4(idx, txtB_Task3Name.Text, txtB_Task3Number.Text);

                isProcessing = true;
                lstV_TaskLevel4.Items.RemoveAt(idx);
                lstV_TaskLevel4.Items.Insert(idx, CreateLI(txtB_Task3Name.Text, txtB_Task3Number.Text));
                //lstV_TaskLevel4.EnsureVisible(idx);
                isProcessing = false;
            }

            dataWH.ClearTaskLevel1();
            lstV_TaskLevel1.Items.Clear();
            txtB_Task1Name.Text = "";
            txtB_Task1Number.Text = "1";

            dataWH.ClearTaskLevel2();
            lstV_TaskLevel2.Items.Clear();
            txtB_Task2Name.Text = "";
            txtB_Task2Number.Text = "1";

            dataWH.ClearTaskLevel3();
            lstV_TaskLevel3.Items.Clear();
            txtB_Task3Name.Text = "";
            txtB_Task3Number.Text = "1";
        }

        private void lstV_TaskLevel1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isProcessing) return;

            ListView lv = (ListView)sender;

            if (lv.SelectedItems.Count != 1)
            {
                txtB_TaskName.Text = "";
                cB_PosClick.SelectedIndex = -1;
                txtB_ClickNumber.Text = "1";
                txtB_ClickInterval.Text = txtB_ClickIntervalDefault.Text;
                txtB_ClickOffset.Text = "0";
                txtB_ClickOffsetNumber.Text = "0";
                return;
            }

            int idx = lv.SelectedItems[0].Index;
            TaskLevel1 tskLvl1 = dataWH.GetTaskLevel1(idx);

            txtB_TaskName.Text = tskLvl1.name;
            cB_PosClick.SelectedIndex = lstV_Pos.Items[tskLvl1.PosName].Index;
            txtB_ClickNumber.Text = tskLvl1.ClickNumber;
            txtB_ClickInterval.Text = tskLvl1.ClickInterval;
            txtB_ClickOffset.Text = tskLvl1.ClickOffset;
            txtB_ClickOffsetNumber.Text = tskLvl1.ClickOffsetNumber;
        }

        private void lstV_TaskLevel2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isProcessing) return;

            ListView lv = (ListView)sender;
            lstV_TaskLevel1.Items.Clear();
            txtB_Task1Name.Text = "";
            txtB_Task1Number.Text = "1";

            dataWH.ClearTaskLevel1();

            if (lv.SelectedItems.Count != 1) return;

            int idx = lv.SelectedItems[0].Index;
            TaskLevel2 tskLvl2 = dataWH.GetTaskLevel2(idx);

            dataWH.LoadTaskLevel1(tskLvl2.arrTskLvl1);
            
            foreach (TaskLevel1 tskLvl1 in dataWH.LST_TSKLVL1)
            {
                lstV_TaskLevel1.Items.Add(CreateLI(tskLvl1.name, tskLvl1.PosName));
            }
            txtB_Task1Name.Text = tskLvl2.name;
            txtB_Task1Number.Text = tskLvl2.arrTskLvl1_Count;
        }

        private void lstV_TaskLevel3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isProcessing) return;

            ListView lv = (ListView)sender;
            lstV_TaskLevel1.Items.Clear();
            txtB_Task1Name.Text = "";
            txtB_Task1Number.Text = "1";
            lstV_TaskLevel2.Items.Clear();
            txtB_Task2Name.Text = "";
            txtB_Task2Number.Text = "1";

            dataWH.ClearTaskLevel2();

            if (lv.SelectedItems.Count != 1) return;

            int idx = lv.SelectedItems[0].Index;
            TaskLevel3 tskLvl3 = dataWH.GetTaskLevel3(idx);

            dataWH.LoadTaskLevel2(tskLvl3.arrTskLvl2);

            foreach (TaskLevel2 tskLvl2 in dataWH.LST_TSKLVL2)
            {
                lstV_TaskLevel2.Items.Add(CreateLI(tskLvl2.name, tskLvl2.arrTskLvl1_Count));
            }
            txtB_Task2Name.Text = tskLvl3.name;
            txtB_Task2Number.Text = tskLvl3.arrTskLvl2_Count;
        }

        private void lstV_TaskLevel4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isProcessing) return;

            ListView lv = (ListView)sender;
            lstV_TaskLevel1.Items.Clear();
            txtB_Task1Name.Text = "";
            txtB_Task1Number.Text = "1";
            lstV_TaskLevel2.Items.Clear();
            txtB_Task2Name.Text = "";
            txtB_Task2Number.Text = "1";
            lstV_TaskLevel3.Items.Clear();
            txtB_Task3Name.Text = "";
            txtB_Task3Number.Text = "1";
            dataWH.ClearTaskLevel3();

            if (lv.SelectedItems.Count != 1)
            {
                txtB_TaskCheck.Text = "";
                return;
            }

            int idx = lv.SelectedItems[0].Index;
            if (lv.SelectedItems[0].Name != DANGNHAP)
            {
                TaskLevel4 tskLvl4 = dataWH.GetTaskLevel4(idx);

                dataWH.LoadTaskLevel3(tskLvl4.arrTskLvl3);

                foreach (TaskLevel3 tskLvl3 in dataWH.LST_TSKLVL3)
                {
                    lstV_TaskLevel3.Items.Add(CreateLI(tskLvl3.name, tskLvl3.arrTskLvl2_Count));
                }
                txtB_Task3Name.Text = tskLvl4.name;
                txtB_Task3Number.Text = tskLvl4.arrTskLvl3_Count;
            }
            else
            {
                TaskLogin tskLvl4 = dataWH.GetTaskLogin(idx);
                txtB_WebAddress.Text = tskLvl4.webAddress;
                cB_PosUsername.SelectedIndex = lstV_Pos.Items[tskLvl4.usrName_Pos].Index;
                txtB_Username.Text = tskLvl4.usrName;
                txtB_Password.Text = tskLvl4.pass;
                cB_PostLogin.SelectedIndex = lstV_Pos.Items[tskLvl4.login_Pos].Index;
                cB_PosLogOut.SelectedIndex = lstV_Pos.Items[tskLvl4.logout_Pos].Index;
            }
            txtB_TaskCheck.Text = lv.SelectedItems[0].Name;
        }

        private void lstV_TaskLevel_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
        
        private void lstV_TaskLevel_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListView lv = (ListView)sender;
            lv.DoDragDrop(lv.SelectedItems, DragDropEffects.Move);
            fromLst = lv.Name;
        }

        private void lstV_TaskLevel_DragOver(object sender, DragEventArgs e)
        {
            ListView lv = (ListView)sender;
            Point pos = lv.PointToClient(new Point(e.X,e.Y));

            ListViewItem itm = lv.GetItemAt(pos.X, pos.Y);
            if (itm != null)
            {
                lv.EnsureVisible(itm.Index);
                e.Effect = DragDropEffects.Move;
            }
        }

        private void lstV_TaskLevel_DragDrop(object sender, DragEventArgs e)
        {
            ListView lv = (ListView)sender;
            if (lv.Name != fromLst)
                return;

            Point cp = lv.PointToClient(new Point(e.X, e.Y));
            ListViewItem dragToItem = lv.GetItemAt(cp.X, cp.Y);
            int dropIndex = -1;
            if (dragToItem != null)
                dropIndex = dragToItem.Index;
            else
                dropIndex = (lv.Items.Count == 0) ? 0 : lv.Items.Count - 1;
            
            int idx = dropIndex;

            foreach (ListViewItem li in (ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)))
            {
                if (lv.Name == lstV_TaskLevel1.Name) dataWH.MoveTaskLevel1(li.Index, dropIndex);
                else if (lv.Name == lstV_TaskLevel2.Name) dataWH.MoveTaskLevel2(li.Index, dropIndex);
                else if (lv.Name == lstV_TaskLevel3.Name) dataWH.MoveTaskLevel3(li.Index, dropIndex);
                else if (lv.Name == lstV_TaskLevel4.Name)
                {
                    if (li.Name == DANGNHAP) dataWH.MoveTaskLogin(li.Index, dropIndex);
                    else dataWH.MoveTaskLevel4(li.Index, dropIndex);
                }

                if (li.Index > dropIndex)
                {
                    idx = dropIndex;
                    dropIndex++;
                }

                li.Remove();
                lv.Items.Insert(idx, li);
            }
        }

        private void lstV_TaskLevel_KeyDown(object sender, KeyEventArgs e)
        {
            ListView lv = (ListView)sender;
            isProcessing = true;
            switch (e.KeyCode)
            {
                case Keys.Delete:
                    if (lv.Name == lstV_TaskLevel4.Name)
                    {
                        lstV_TaskLevel3.Items.Clear();
                        lstV_TaskLevel2.Items.Clear();
                        lstV_TaskLevel1.Items.Clear();
                        txtB_Task1Name.Text = "";
                        txtB_Task1Number.Text = "1";
                        txtB_Task2Name.Text = "";
                        txtB_Task2Number.Text = "1";
                        txtB_Task3Name.Text = "";
                        txtB_Task3Number.Text = "1";
                    }
                    else if (lv.Name == lstV_TaskLevel3.Name)
                    {
                        lstV_TaskLevel2.Items.Clear();
                        lstV_TaskLevel1.Items.Clear();
                        txtB_Task1Name.Text = "";
                        txtB_Task1Number.Text = "1";
                        txtB_Task2Name.Text = "";
                        txtB_Task2Number.Text = "1";
                        
                    }
                    else if (lv.Name == lstV_TaskLevel2.Name)
                    {
                        lstV_TaskLevel1.Items.Clear();
                        txtB_Task1Name.Text = "";
                        txtB_Task1Number.Text = "1";
                    }

                    foreach (ListViewItem li in lv.SelectedItems)
                    {
                        if (lv.Name == lstV_TaskLevel1.Name)
                                dataWH.RemoveTaskLevel1(li.Index);
                        else if (lv.Name ==  lstV_TaskLevel2.Name)
                                dataWH.RemoveTaskLevel2(li.Index);
                        else if (lv.Name == lstV_TaskLevel3.Name)
                            dataWH.RemoveTaskLevel3(li.Index);
                        else if (lv.Name == lstV_TaskLevel4.Name)
                        {
                            dataWH.RemoveTaskLevel4(li.Index);
                            if (li.Name == DANGNHAP) dataWH.TASKLOGININDEX = -1;
                        }
                        lv.Items.Remove(li);
                    }
                    break;
                case Keys.C:
                    if (e.Control)
                    { //ctrl + C
                        copyItems(lv);
                    }
                    break;
                case Keys.V:
                    if (e.Control)
                    {
                        pasteItems(lv);
                    }
                    break;
            }

            isProcessing = false;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            dataWH.RUNNUMBER = txtB_Task4Number.Text;
            dataWH.SetProgramSettings(txtB_ClickIntervalDefault.Text, txtB_TaskInterval.Text);
            dataWH.SaveData();
        }

        private void btn_Run_Click(object sender, EventArgs e)
        {
            if (menu_Run.Text == "Chạy Click")
            {              
                this.WindowState = FormWindowState.Minimized;

                rClk = new RunClick(ref dataWH,true);
                runThread = new Thread(new ThreadStart(rClk.RunTasks));
                runThread.Start();
                menu_Run.Text = "Dừng chạy";
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void btn_RunTaskCheck_Click(object sender, EventArgs e)
        {
            if (txtB_TaskCheck.Text == "") return;

            if (menu_RunTest.Text == "Chạy Test")
            {
                this.WindowState = FormWindowState.Minimized;

                rClk = new RunClick(ref dataWH, true,lstV_TaskLevel4.Items.IndexOfKey(txtB_TaskCheck.Text));
                runThread = new Thread(new ThreadStart(rClk.RunTaskTest));
                runThread.Start();
                menu_RunTest.Text = "Dừng chạy test";
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void menu_OpenWindow_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                menu_RunTest.Text = "Chạy Test";
                menu_Run.Text = "Chạy Click";
                rClk.RunState = false;
            }
        }

        private void menu_CopyItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
            ContextMenuStrip strip = (ContextMenuStrip)menuItem.Owner;
            ListView lv = (ListView)(strip.SourceControl);

            copyItems(lv);
        }

        private void copyItems(ListView lv)
        {
            if (lv.SelectedItems.Count <= 0) return;

            copyFrom = lv.Name;
            dataWH.ClearTaskCopied();

            string ext = txtB_CopyLabel.Text;

            foreach (ListViewItem li in lv.SelectedItems)
            {
                if (lv.Name == lstV_TaskLevel1.Name) dataWH.CopyTaskLevel1(li.Index, ext);
                else if (lv.Name == lstV_TaskLevel2.Name) dataWH.CopyTaskLevel2(li.Index, ext);
                else if (lv.Name == lstV_TaskLevel3.Name) dataWH.CopyTaskLevel3(li.Index, ext);
                else if (lv.Name == lstV_TaskLevel4.Name)
                {
                    if (li.Name != DANGNHAP) dataWH.CopyTaskLevel4(li.Index, ext);
                }
            }
        }

        private void menu_PasteItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
            ContextMenuStrip strip = (ContextMenuStrip)menuItem.Owner;
            ListView lv = (ListView)(strip.SourceControl);

            pasteItems(lv);
        }

        private void pasteItems(ListView lv)
        {
            if (lv.Name != copyFrom) return;

            ArrayList pastedItems = new ArrayList();

            if (lv.Name == lstV_TaskLevel1.Name) pastedItems = dataWH.PasteTasksLevel1();
            else if (lv.Name == lstV_TaskLevel2.Name) pastedItems = dataWH.PasteTasksLevel2();
            else if (lv.Name == lstV_TaskLevel3.Name) pastedItems = dataWH.PasteTasksLevel3();
            else if (lv.Name == lstV_TaskLevel4.Name) pastedItems = dataWH.PasteTasksLevel4();

            for (int i = 0; i < pastedItems.Count; i += 2)
            {
                lv.Items.Add(CreateLI((string)pastedItems[i], (string)pastedItems[i + 1]));
            }
            dataWH.ClearTaskCopied();
        }

        private void menu_ListViewOpening(object sender, CancelEventArgs e)
        {
            ContextMenuStrip strip = (ContextMenuStrip)sender;
            ListView lv = (ListView)(strip.SourceControl);
            
            if (dataWH.LST_TSKCOPIED.Count > 0 && lv.Name == copyFrom)
                menu_PasteItem.Enabled = true;
            else
                menu_PasteItem.Enabled = false;
        }

        private void btnGetColor_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(int.Parse(txtB_WaitPos.Text));

            Point pos = new Point();
            Color clr;
            if (txtB_ColorPos.Text == "-1,-1")
            {
                pos = System.Windows.Forms.Cursor.Position;;
                clr = rClk.GetColorAtMouseClick();
            }
            else
            {
                string[] strSplit = txtB_ColorPos.Text.Split(',');
                pos.X = int.Parse(strSplit[0]);
                pos.Y = int.Parse(strSplit[1]);
                clr = rClk.GetColorAtMouseClick(pos.X, pos.Y);
            }
            txtB_Pos.Text = "-4," + clr.A.ToString() + "," + clr.R.ToString() + "," + clr.G.ToString() + "," + clr.B.ToString();
            txtB_ColorPos.Text = pos.X + "," + pos.Y;

            this.WindowState = FormWindowState.Normal;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            Web_browser wb = new Web_browser();
            wb.ShowDialog();
        }

        private void btn_ImageRelation_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            PlayImageRelation playImg = new PlayImageRelation();
            playImg.WindowState = FormWindowState.Normal;
            playImg.TopMost = true;
            playImg.ShowDialog();
            //Application.OpenForms[
            this.WindowState = FormWindowState.Normal;
        }
    }
}
